<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.appspot.niehonglei.jdo.GuestBook" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.appspot.niehonglei.dao.PMF" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板</title>
<style type="text/css">
<!--
-->
</style>
<script type="text/javascript">
function isKeyTrigger(e,keyCode){
    var argv = isKeyTrigger.arguments;
    var argc = isKeyTrigger.arguments.length;
    var bCtrl = false;
    if(argc > 2){
        bCtrl = argv[2];
    }
    var bAlt = false;
    if(argc > 3){
        bAlt = argv[3];
    }

    var nav4 = window.Event ? true : false;

    if(typeof e == 'undefined') {
        e = event;
    }

    if( bCtrl &&
        !((typeof e.ctrlKey != 'undefined') ?
            e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)){
        return false;
    }
    if( bAlt &&
        !((typeof e.altKey != 'undefined') ?
            e.altKey : e.modifiers & Event.ALT_MASK > 0)){
        return false;
    }
    var whichCode = 0;
    if (nav4) whichCode = e.which;
    else if (e.type == "keypress" || e.type == "keydown")
        whichCode = e.keyCode;
    else whichCode = e.button;

    return (whichCode == keyCode);
}

function ctrlEnter(e){
    var ie =navigator.appName=="Microsoft Internet Explorer"?true:false;
    if(ie){
        if(event.ctrlKey && window.event.keyCode==13){doSomething();}
    }else{
        if(isKeyTrigger(e,13,true)){doSomething();}
    }
}
function doSomething(){document.forms[0].submit(); }
</script>
</head>

<body>

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null && "niehonglei".equals(user.getNickname())) {
%>
<form id="form1" name="form1" method="post" action="/action/saveGuestBook"><p><a href="<%= userService.createLogoutURL("/guestbook/") %>">sign out</a></p>
  <p>输入要发布的新闻：<br />
    <textarea name="content" id="content" onkeyup="javascript:return ctrlEnter(event);" cols="45" rows="5"></textarea>
  </p>
<hr />
  <%
	}else{
		%><a href="<%= userService.createLoginURL("/guestbook/") %>">Sign in</a>
		<%
	}
	%>
  <%
  		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + GuestBook.class.getName()
				+ " order by date desc range 0,10";
		List<GuestBook> list = (List<GuestBook>) pm.newQuery(query)
				.execute();
		if(list==null) return;
		for(int i=0;i<list.size();i++){
			GuestBook guestBook = list.get(i);
			out.println("<p>" + guestBook.getContent().getValue() + "</p>");
		}
  %>
</form>
</body>
</html>